import pandas as pd
import plotly.express as px
from datetime import datetime

# === 设置真实周期（单位：天） ===
true_period = 0.394

# === 加载 CSV 数据文件 ===
file_path = "/mnt/7b21f1e1-eb25-4cd5-bdb5-06d7d82fa253/Temp/force_photmetry/result/asteroid_photometry_results.csv"
df = pd.read_csv(file_path)

# 准备每个波段的数据
def prepare_band(df, band):
    df_band = df[(df['band'] == band) & (df['calmag'].notna())].copy()
    df_band['timestamp'] = pd.to_datetime(df_band['obs_time'])
    df_band['mjd'] = (df_band['timestamp'] - df_band['timestamp'].min()).dt.total_seconds() / 86400.0
    df_band['phase'] = (df_band['mjd'] / true_period) % 1
    df_band['hover_info'] = df_band.apply(
        lambda row: f"file: {row['fn']}<br>time: {row['obs_time']}<br>mag: {row['calmag']:.3f} ± {row['magerr']:.3f}",
        axis=1
    )
    return df_band

# 处理 u, v, i, z 波段
bands = ['u', 'v', 'i', 'z']
prepared_data = {band: prepare_band(df, band) for band in bands}

# 生成每个波段的 HTML 图表
html_outputs = {}
for band, band_df in prepared_data.items():
    fig = px.scatter(
        band_df, x='phase', y='calmag', error_y='magerr',
        hover_data=['hover_info'], title=f"{band}-band Folded Light Curve (P = {true_period} days)",
        labels={'calmag': 'Magnitude', 'phase': 'Phase'},
        color_discrete_sequence=['blue']
    )
    fig.update_yaxes(autorange="reversed")
    fig.update_traces(marker=dict(size=6, opacity=0.8), hovertemplate="%{customdata[0]}")
    html_file = f"lightcurve_{band}.html"
    fig.write_html(html_file)
    html_outputs[band] = html_file